﻿var Product = function (id, name, fieldName, price) {
    this.id = ko.observable(id);
    this.name = ko.observable(name);
    this.fieldName = ko.observable(fieldName);
    this.price = ko.observable(price);
};

var CartModel = function() {
    var self = this; // scope

    /**
    * Variables
    **/
    self.sales_tax = 0.07;

    /**
    * Arrays
    **/
    self.cart = ko.observableArray();

    /** 
    * Computed Observables
    **/
    self.tax = ko.computed(function() {

    });

    self.total = ko.computed(function() {
        var total = 0;
        $.each(self.cart(), function() {
            total += this.price();
        });
        return total;
    });

    /**
    * Actions
    **/
    self.addItem = function (data, event) {
        // get the button so we can retrieve values
        var t = $(event.target);
        // create a product
        var prod = new Product(parseInt(t.attr('prodId')), t.attr('product'), t.attr('fieldName'), parseInt(t.attr('price')));
        // add to cart (check it hasn't been added already, maybe do this server side and decide whether to add then, actually no that is server work
        // we dont want
        self.cart.push(prod);
    };
}

$(function() {
    ko.applyBindings(new CartModel());
})